package com.region.plugin.proxy.jdk;

import com.region.common.adapter.logging.LoggerAdapter;
import com.region.common.adapter.logging.LoggerAdapterFactory;
import com.region.common.utils.ClassUtils;
import com.region.plugin.Interceptor;
import com.region.plugin.ProxyTypeRegister;
import com.region.plugin.proxy.ProxyHandler;

import java.lang.reflect.Proxy;

/**
 * JDK Dynamic Proxy Implementation Classes
 *
 * @author liujieyu
 * @date 2023/5/23 20:27
 * @desciption
 */
public class JdkProxyHandler implements ProxyHandler {

    private final LoggerAdapter logger = LoggerAdapterFactory.getLogger(getClass());

    @Override
    public Object createProxyInstance(Object target, Class<?>[] classes, Interceptor interceptor, Object[] args) throws UnsupportedOperationException {
        if (interceptor == null) {
            return target;
        }
        if (classes == null || classes.length == 0) {
            logger.warn("The proxy class does not have an interface, Returns the proxy class directly");
            return target;
        }
        return Proxy.newProxyInstance(ClassUtils.getClassLoader(), classes, new JdkRealProxy(target, interceptor));
    }

}
